<?php

namespace addons\crontab\controller;

use addons\crontab\model\Crontab;
use app\admin\model\inventory\Kc;
use app\admin\model\Kisapi;
use Cron\CronExpression;
use fast\Http;
use think\Controller;
use think\Db;
use think\Exception;
use think\Log;

/**
 * 定时任务接口
 *
 * 以Crontab方式每分钟定时执行,且只可以Cli方式运行
 * @internal
 */
class Material extends Controller
{

    /**
     * 初始化方法,最前且始终执行
     */
    public function _initialize()
    {
        // 只可以以cli方式执行
        if (!$this->request->isCli()) {
            $this->error('Autotask script only work at client!');
        }

        parent::_initialize();

        // 清除错误
        error_reporting(0);

        // 设置永不超时
        set_time_limit(0);
    }

    /**
     * 执行定时任务
     */
    public function index()
    {
        $this->getData();
        return;
    }

    /**
     * 同步数据
     */
    public function getData()
    {
        try {
            $material = new \app\admin\model\inventory\Material;

            $material->getMaterialData('','',3);//销售出库单
            
            Log::info('计划任务-领料同步完成');
            return ;
            //这块需求，已作废。获取到销售出库单，再到pda做销售出库单指定批号，再回写，更新销售出库单的金蝶明细
            //获取配置日期段
            $material_sel_month = intval(cache('material_sel_month'));
            $date_arr = $material::getFahuoCfgDate();
            //按月分割
            $monthArr = getDateByInterval($date_arr['StartTime'],$date_arr['EndTime'],'month');
            
            $material_sel_month++;
            if($material_sel_month>count($monthArr)){
                $material_sel_month = 0;
            }
            if(!isset($monthArr[$material_sel_month])){
                $material_sel_month = 0;
            }
            cache('material_sel_month',$material_sel_month);

            $param['StartTime'] = $monthArr[$material_sel_month]['startDate'];
            $param['EndTime'] = $monthArr[$material_sel_month]['endDate'];

            $material->getMaterialData('','',1,$param);//发货通知单
            $material->getMaterialData('','',1);//发货通知单 近1个月
            
            Log::info($param,'计划任务-领料同步完成,日期');
        }catch (Exception $e){
            Log::error('计划任务-领料同步失败'.$e->getMessage());
        }
    }

    /**
     * 处理销售出库单数据
     */ 
    public function dealData(){
        //本地的销售出库单，在金蝶搜索，得到状态是审核、找不到的，删除本地
        $material_codes = db('material_list')->where([
            'status' => 1
        ])
        ->group('material_code')
        ->column('material_code');
        
        if($material_codes){
            foreach($material_codes as $material_code){
                $condition = [
                    "fAcctNum" => "001",//味业账套
                    "fTrantype" => 21,
                    "fBillNo" => $material_code,
                ];
                $api_name = 'getIcStockbill';
                $erp_detail = Kisapi::getDataByName($api_name,$condition,false);
                
                $deleteflag = false;
                if(!$erp_detail){
                    $deleteflag = true;
                }else{
                    if($erp_detail['fStatus']){
                        $deleteflag = true;
                    }
                }

                if($deleteflag){
                    db('material')->where('material_code',$material_code)->delete();
                    db('material_list')->where('material_code',$material_code)->delete();
                }
            }
        }
    }
}
